//
//  HomeData.m
//  Village
//
//  Created by 程虎 on 2017/9/19.
//  Copyright © 2017年 程虎. All rights reserved.
//

#import "HomeHttpTools.h"

@implementation HomeHttpTools

+ (void)GetAreaIDWith:(LocationModel *)model Success:(void (^)(NSString *arearid))success failure:(void (^)(NSString *))failure
{
    NSMutableDictionary *bean = [NSMutableDictionary dictionary];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [bean setValue:dict forKey:@"entitybean"];
    [dict setObject:model.State.length > 0 ? model.State : @"" forKey:@"province"];
    [dict setObject:model.City.length > 0 ? model.City : @"" forKey:@"city"];
    [dict setObject:model.SubLocality.length > 0 ? model.SubLocality : @"" forKey:@"county"];
    [NetRequestClass Post:GetAreaIDURL Parameters:bean success:^(id result) {
        [[Globle getInstance] saveAreaid:result[@"data"][@"areaid"]];
        if (success) {
            success(result[@"data"][@"areaid"]);
        }
    } failure:^(id valueCode, id error) {
        if (failure) {
            failure(error);
        }
        NSLog(@"areaid error -----  %@",error);
    }];
}

+ (void)GetBannerSuccess:(void (^)(NSArray *valueAry,NSArray *imgArys,NSArray *titleArys))success failure:(void (^)(NSString *error))failure
{
    NSMutableDictionary *bean = [NSMutableDictionary dictionary];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [bean setValue:dict forKey:@"entitybean"];
    [dict setObject:@"3" forKey:@"pagesize"];
    [dict setObject:@"1" forKey:@"pagenum"];
    [NetRequestClass Post:HomeBannerInfoURL Parameters:bean success:^(id result) {
        if (success) {
            NSArray *arrays = [VillageModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"list"]];
            NSArray *arrays1 = [HomeHttpTools configIMGData:arrays];
            NSArray *arrays2 = [HomeHttpTools configTitleData:arrays];
            success(arrays,arrays1,arrays2);
        }
    } failure:^(id valueCode, id error) {
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)GetHeadlineSuccess:(void (^)(NSArray *valueAry))success failure:(void (^)(NSString *))failure
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
    });
    NSMutableDictionary *bean = [NSMutableDictionary dictionary];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [bean setValue:dict forKey:@"entitybean"];
    [dict setObject:@"5" forKey:@"pagesize"];
    [dict setObject:@"1" forKey:@"pagenum"];
    [dict setObject:[Globle getInstance].account.areaid.length > 0 ? [Globle getInstance].account.areaid : ([Globle getInstance].areaid.length > 0 ? [Globle getInstance].areaid :@"") forKey:@"areaid"];
    [NetRequestClass Post:HomeHeadlineInfoURL Parameters:bean success:^(id result) {
        if (success) {
            success([VillageModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"list"]]);
        }
    } failure:^(id valueCode, id error) {
        if (failure) {
            failure(error);
        }
    }];
}

/**
 获取区域id
 */
+ (void)AddHeadlineNewReadNumWith:(VillageModel *)model Success:(void (^)())success failure:(void (^)(NSString *error))failure
{
    NSMutableDictionary *bean = [NSMutableDictionary dictionary];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [bean setValue:dict forKey:@"entitybean"];
    [dict setObject:model.newsid forKey:@"id"];
    [NetRequestClass Post:AddHeadlineNewsReadNumURL Parameters:bean success:^(id result) {
        if (success) {
            success();
        }
    } failure:^(id valueCode, id error) {
        if (failure) {
            failure(error);
        }
    }];
}

+ (NSMutableArray *)configIMGData:(NSArray *)arrays
{
    NSMutableArray *datas = [NSMutableArray array];
    for (int i = 0; i < arrays.count; i++) {
        VillageModel *model = arrays[i];
        NSArray *arys = [Tools leapImgAry:[Tools toJSONData:model.upimg]];
        if (arys.count > 0) {
            [datas addObject:arys[0]];
        }
    }
     return datas;
}

+ (NSMutableArray *)configTitleData:(NSArray *)arrays
{
    NSMutableArray *datas = [NSMutableArray array];
    for (VillageModel *model in arrays) {
        [datas addObject:model.title];
    }
    return datas;
}
@end
